﻿using Little.Soldier;
using Little.Soldier.SWIFT.Validation;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Little.Soldier.SWIFT
{
  [DesignTimeVisible(false)]
  [ToolboxItem(false)]
  [TypeConverter(typeof(ISwiftBlock4))]
  [Serializable]
  public class SwiftBlock4 : SwiftTagListBlock
  {
    private const long _SerialVersionUID = 1L;

    public override string BlockName
    {
      get
      {
        return "4";
      }
    }

    public override int BlockNumber
    {
      get
      {
        return 4;
      }
    }

    public SwiftBlock4(SwiftMessage swiftMessage)
      : base(swiftMessage)
    {
    }

    public SwiftBlock4(SwiftMessage swiftMessage, SwiftTag[] tags)
      : base(swiftMessage)
    {
      foreach (SwiftTag swiftTag in tags)
        this.Tags.Add(swiftTag);
    }

    public override void InitializeRules()
    {
    }

    public override bool Validate(List<ValidationError> errorList)
    {
      if (errorList == null)
        errorList = new List<ValidationError>();
      int count = errorList.Count;
      foreach (IValidate validate in this.Rules)
      {
        if (validate is ValidateTagBase)
          ((ValidateTagBase) validate).Tags = this.GetTagsByName(((ValidateTagBase) validate).TagName);
      }
      base.Validate(errorList);
      this.SwiftMessage.IsBlock4Error(errorList);
      return count >= errorList.Count;
    }

    public virtual bool ContainsTag(int number)
    {
      if (this.Tags != null && this.Tags.Count != 0)
      {
        foreach (SwiftTag swiftTag in this.Tags)
        {
          if (swiftTag.IsNumber(number))
            return true;
        }
      }
      return false;
    }

    public new virtual SwiftTag GetTagByNumber(int number)
    {
      if (this.Tags != null && this.Tags.Count != 0)
      {
        foreach (SwiftTag swiftTag in this.Tags)
        {
          if (swiftTag.IsNumber(number))
            return swiftTag;
        }
      }
      return (SwiftTag) null;
    }

    public virtual List<SwiftTag> GetTagsByNumber(int number)
    {
      List<SwiftTag> list = new List<SwiftTag>();
      if (this.Tags != null && this.Tags.Count != 0)
      {
        foreach (SwiftTag swiftTag in this.Tags)
        {
          if (swiftTag.IsNumber(number))
            list.Add(swiftTag);
        }
      }
      return list;
    }
  }
}
